/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rc;

import java.util.ArrayList;
import java.util.List;
import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.Variable;
import tal.remotecanvas.CanvasPacket;
import tal.remotecanvas.CanvasPacket.ALIGN;
import tal.remotecanvas.CanvasPacket.FEAT;
import tal.remotecanvas.PacketFactory;
import tal.remotecanvas.rcColor;

/**
 *
 * @author Tal Eisenberg
 */
public class Messages extends rcModule {
    boolean batchMode = false;
    List<CanvasPacket> batch = new ArrayList<CanvasPacket>();

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
    }

    public void send(CanvasPacket packet) {
        if (batchMode) {
            batch.add(packet);
        } else {
            sendToOutputs(this.packetToVar(packet), -1);
        }
    }

    public void batch()  {
        this.batchMode = true;
        batch.clear();
    }

    public void endbatch() {
        batchMode = false;
        send(PacketFactory.batch(batch.toArray(new CanvasPacket[0])));
    }

    public void clear() {
        send(PacketFactory.clear());
    }

    public void clearrect(short x1, short y1, short x2, short y2) {
        send(PacketFactory.clearrect(x1, y1, x2, y2));
    }

    public void brgb(short red, short green, short blue, short alpha) {
        send(PacketFactory.brgb(new rcColor(red, green, blue, alpha)));
    }
    public void brgb(short red, short green, short blue) {
        brgb(red, green, blue, (short)255);
    }

    public void frgb(short red, short green, short blue, short alpha) {
        send(PacketFactory.frgb(new rcColor(red, green, blue, alpha)));
    }
    public void frgb(short red, short green, short blue) {
        frgb(red, green, blue, (short)255);
    }

    public void frameoval(short x1, short y1, short x2, short y2, short red, short green, short blue) {
        frameoval(x1, y1, x2, y2, red, green, blue, (short)255);
    }
    public void frameoval(short x1, short y1, short x2, short y2, short red, short green, short blue, short alpha) {
        send(PacketFactory.frameoval(x1, y1, x2, y2, new rcColor(red, green, blue, alpha)));
    }
    public void frameoval(short x1, short y1, short x2, short y2) {
        send(PacketFactory.frameoval(x1, y1, x2, y2));
    }

    public void framerect(short x1, short y1, short x2, short y2, short red, short green, short blue) {
        framerect(x1, y1, x2, y2, red, green, blue, (short)255);
    }
    public void framerect(short x1, short y1, short x2, short y2, short red, short green, short blue, short alpha) {
        send(PacketFactory.framerect(x1, y1, x2, y2, new rcColor(red, green, blue, alpha)));
    }
    public void framerect(short x1, short y1, short x2, short y2) {
        send(PacketFactory.framerect(x1, y1, x2, y2));
    }

    public void framepoly(short[] coords) {
        send(PacketFactory.framepoly(coords));
    }

    public void framearc(short x1, short y1, short x2, short y2, double startangle, double endangle, short red, short green, short blue) {
        framearc(x1, y1, x2, y2, startangle, endangle, red, green, blue, (short)255);
    }
    public void framearc(short x1, short y1, short x2, short y2, double startangle, double endangle, short red, short green, short blue, short alpha) {
        send(PacketFactory.framearc(x1, y1, x2, y2, startangle, endangle, new rcColor(red, green, blue, alpha)));
    }
    public void framearc(short x1, short y1, short x2, short y2, double startangle, double endangle) {
        send(PacketFactory.framearc(x1, y1, x2, y2, startangle, endangle));
    }

    public void frameroundrect(short x1, short y1, short x2, short y2, short horizonRound, short vertRound, short red, short green, short blue) {
        this.frameroundrect(x1, y1, x2, y2, horizonRound, vertRound, red, green, blue, (short)255);
    }
    public void frameroundrect(short x1, short y1, short x2, short y2, short horizonRound, short vertRound, short red, short green, short blue, short alpha) {
        send(PacketFactory.frameroundrect(x1, y1, x2, y2, horizonRound, vertRound, new rcColor(red, green, blue, alpha)));
    }
    public void frameroundrect(short x1, short y1, short x2, short y2, short horizonRound, short vertRound) {
        send(PacketFactory.frameroundrect(x1, y1, x2, y2, horizonRound, vertRound));
    }

    public void linesegment(short x1, short y1, short x2, short y2, short red, short green, short blue) {
        linesegment(x1, y1, x2, y2, red, green, blue, (short)255);
    }
    public void linesegment(short x1, short y1, short x2, short y2, short red, short green, short blue, short alpha) {
        send(PacketFactory.linesegment(x1, y1, x2, y2, new rcColor(red, green, blue, alpha)));
    }
    public void linesegment(short x1, short y1, short x2, short y2) {
        send(PacketFactory.linesegment(x1, y1, x2, y2));
    }

    public void paintoval(short x1, short y1, short x2, short y2, short red, short green, short blue) {
        paintoval(x1, y1, x2, y2, red, green, blue, (short)255);
    }
    public void paintoval(short x1, short y1, short x2, short y2, short red, short green, short blue, short alpha) {
        send(PacketFactory.paintoval(x1, y1, x2, y2, new rcColor(red, green, blue, alpha)));
    }
    public void paintoval(short x1, short y1, short x2, short y2) {
        send(PacketFactory.paintoval(x1, y1, x2, y2));
    }

    public void paintpoly(short[] coords) {
        send(PacketFactory.paintpoly(coords));
    }

    public void paintrect(short x1, short y1, short x2, short y2, short red, short green, short blue) {
        paintrect(x1, y1, x2, y2, red, green, blue, (short)255);
    }
    public void paintrect(short x1, short y1, short x2, short y2, short red, short green, short blue, short alpha) {
        send(PacketFactory.paintrect(x1, y1, x2, y2, new rcColor(red, green, blue, alpha)));
    }
    public void paintrect(short x1, short y1, short x2, short y2) {
        send(PacketFactory.paintrect(x1, y1, x2, y2));
    }

    public void paintarc(short x1, short y1, short x2, short y2, double startangle, double endangle, short red, short green, short blue) {
        paintarc(x1, y1, x2, y2, startangle, endangle, red, green, blue, (short)255);
    }
    public void paintarc(short x1, short y1, short x2, short y2, double startangle, double endangle, short red, short green, short blue, short alpha) {
        send(PacketFactory.paintarc(x1, y1, x2, y2, startangle, endangle, new rcColor(red, green, blue, alpha)));
    }
    public void paintarc(short x1, short y1, short x2, short y2, double startangle, double endangle) {
        send(PacketFactory.paintarc(x1, y1, x2, y2, startangle, endangle));
    }

    public void paintroundrect(short x1, short y1, short x2, short y2, short horizonRound, short vertRound, short red, short green, short blue) {
        this.paintroundrect(x1, y1, x2, y2, horizonRound, vertRound, red, green, blue, (short)255);
    }
    public void paintroundrect(short x1, short y1, short x2, short y2, short horizonRound, short vertRound, short red, short green, short blue, short alpha) {
        send(PacketFactory.paintroundrect(x1, y1, x2, y2, horizonRound, vertRound, new rcColor(red, green, blue, alpha)));
    }
    public void paintroundrect(short x1, short y1, short x2, short y2, short horizonRound, short vertRound) {
        send(PacketFactory.paintroundrect(x1, y1, x2, y2, horizonRound, vertRound));
    }

    public void rcreset() {
        send(PacketFactory.reset());
    }

    public void setpixel(short x, short y, short red, short green, short blue) {
        setpixel(x, y, red, green, blue, (short)255);
    }
    public void setpixel(short x, short y, short red, short green, short blue, short alpha) {
        send(PacketFactory.setpixel(x, y, new rcColor(red, green, blue, alpha)));
    }
    public void setpixel(short x, short y) {
        send(PacketFactory.setpixel(x, y));
    }

    public void move(short x, short y) {
        send(PacketFactory.move(x, y));
    }

    public void moveto(short x , short y) {
        send(PacketFactory.moveto(x, y));
    }

    public void line(short xOffset, short yOffset) {
        send(PacketFactory.line(xOffset, yOffset));
    }

    public void lineto(short x, short y) {
        send(PacketFactory.lineto(x, y));
    }

    public void getpenloc() {
        send(PacketFactory.getpenloc());
    }

    public void pensize(short size) {
        send(PacketFactory.pensize(size));
    }

    public void penmode(short mode) {
        send(PacketFactory.penmode(mode));
    }

    public void getsize() {
        send(PacketFactory.getsize());
    }

    public void ascii(char c) {
        send(PacketFactory.ascii(c));
    }

    public void write(String text) {
        send(PacketFactory.write(text));
    }

    public void writealign(String[] words) {
        if (words.length>2) {
            String text = "";
            for (int i=2; i<words.length; i++) text += words[i] + " ";
            text = text.substring(0, text.length()-1);
            String shor = "hor" + words[0].substring(0, 1).toUpperCase() + words[0].substring(1);
            String svert = "vert" + words[1].substring(0, 1).toUpperCase() + words[1].substring(1);
            ALIGN hor = ALIGN.alignByName(shor);
            ALIGN vert = ALIGN.alignByName(svert);
            send(PacketFactory.write(text, hor, vert));
        } else log("writealign: missing alignment constants.", MSG.ERROR);
    }

    public void font(String name, short size) {
        send(PacketFactory.font(name, size));
    }

    public void online() {
        send(PacketFactory.online());
    }

    public void offline() {
        send(PacketFactory.offline());
    }

    public void enable(Object... args) {
        if (args.length==0) { log("enable: feature name missing.", MSG.ERROR); return; }
        if (!(args[0] instanceof String)) { log("enable: first argument expected to be a symbol.", MSG.ERROR); return; }
        String feature = (String)args[0];

        FEAT feat = null;
        for (FEAT f : FEAT.values()) {
            if (f.name().equals(feature)) {
                feat = f;
                break;
            }
        }
        if (feat==null) log("unknown feature: " + feature, MSG.ERROR);
        short[] sargs = new short[args.length-1];
        for (int i=0; i<sargs.length; i++)
            sargs[i] = (Short)args[i+1];
        send(PacketFactory.enable(feat, sargs));
    }

    public void disable(String feature) {
        for (FEAT f : FEAT.values()) {
            if (f.name().equals(feature)) {
                send(PacketFactory.disable(f));
                return;
            }
        }
        log("unknown feature: " + feature, MSG.ERROR);
    }

    public void resetfeatures() {
        send(PacketFactory.resetfeatures());
    }

    public void landscape(short l) {
        send(PacketFactory.landscape(l));
    }

    public void enablesprites(short e) {
        send(PacketFactory.enablesprites(e));
    }

    public void recordsprite() {
        send(PacketFactory.recordsprite());
    }

    public void recordsprite(String parentName) {
        send(PacketFactory.recordsprite(parentName));
    }

    public void closesprite(String newName) {
        send(PacketFactory.closesprite(newName));
    }

    public void makesprite(String spriteName, String parentName) {
        send(PacketFactory.makesprite(spriteName, parentName));
    }

    public void makesprite(String spriteName) {
        send(PacketFactory.makesprite(spriteName));
    }

    public void makesprite(String spriteName, int width, int height) {
        send(PacketFactory.makesprite(spriteName, width, height));
    }

    public void makesprite(String spriteName, String parentName, int width, int height) {
        send(PacketFactory.makesprite(spriteName, parentName, width, height));
    }

    public void drawsprite(String spriteName, short x, short y) {
        send(PacketFactory.drawsprite(spriteName, x, y));
    }

    public void hidesprite(String spriteName) {
        send(PacketFactory.hidesprite(spriteName));
    }

    public void clearsprites() {
        send(PacketFactory.clearsprites());
    }

    public void deletesprite(String spriteName) {
        send(PacketFactory.deletesprite(spriteName));
    }

    public void backsprite(String spriteName) {
        send(PacketFactory.backsprite(spriteName));
    }

    public void frontsprite(String spriteName) {
        send(PacketFactory.frontsprite(spriteName));
    }

    public void editsprite(String spriteName) {
        send(PacketFactory.editsprite(spriteName));
    }

    public void scalesprite(String spriteName, double rx, double ry) {
        send(PacketFactory.scalesprite(spriteName, rx, ry));
    }

    public void rotatesprite(String spriteName, double angle) {
        send(PacketFactory.rotatesprite(spriteName, angle));
    }

    public void translatesprite(String spriteName, int dx, int dy) {
        send(PacketFactory.translatesprite(spriteName, dx, dy));
    }

    public void undospritetransform(String spriteName) {
        send(PacketFactory.undospritetransform(spriteName));
    }

    public void recordmask(String spriteName, int background) {
        send(PacketFactory.recordmask(spriteName, background));
    }

    public void closemask(String spriteName) {
        send(PacketFactory.closemask(spriteName));
    }

    public void clipmask(String spriteName) {
        send(PacketFactory.clipmask(spriteName));
    }

    public void rotate(double angle) {
        send(PacketFactory.rotate(angle));
    }

    public void scale(double rx, double ry) {
        send(PacketFactory.scale(rx, ry));
    }

    public void translate(int dx, int dy) {
        send(PacketFactory.translate(dx, dy));
    }

    public void undotransform() {
        send(PacketFactory.undotransform());
    }

    public void outaddress(String host, String port) {
        send(PacketFactory.outaddress(host, port));
    }
}
